package com.study.d012Components.listview;

import java.util.Comparator;
import java.util.function.Predicate;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/** 列表查找排序
*@description
*@author xtayhame
*date 2020年3月14日
*version
*since v1.0
*/
public class ListViewWithSearchSort extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();

		ObservableList<Student> datas=FXCollections.observableArrayList();
		datas.add(new Student("A123", 18));
		datas.add(new Student("B321", 19));
		datas.add(new Student("C345", 20));
		datas.add(new Student("D537", 21));
		datas.add(new Student("E", 22));
		datas.add(new Student("F", 23));
		datas.add(new Student("G", 24));

		ListView<Student> listView = new ListView<Student>(datas);

		Label searchLabel = new Label("搜索");
		TextField searchField = new TextField();
		Button sortButton = new Button("排序");

		HBox hBox = new HBox(10);
		hBox.setAlignment(Pos.CENTER);
		hBox.getChildren().addAll(searchLabel,searchField,sortButton);

		VBox  vBox = new VBox(20);
		vBox.setAlignment(Pos.CENTER);
		vBox.getChildren().addAll(hBox,listView);

		listView.setPrefSize(300, 400);

		anchorPane.getChildren().addAll(vBox);
		AnchorPane.setLeftAnchor(vBox, 40.0);
		AnchorPane.setTopAnchor(vBox, 20.0);

		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();


		listView.setCellFactory(TextFieldListCell.forListView(new StringConverter<Student>() {

			@Override
			public String toString(Student object) {

				return object.getName() + "-" + object.getAge();
			}

			@Override
			public Student fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		}));


		//搜索
		searchField.textProperty().addListener(new ChangeListener<String>() {

			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				//这里使用filtered()方法
				FilteredList<Student> ft=datas.filtered(new Predicate<Student>() {
					@Override
					public boolean test(Student t) {
						if(t.getName().contains(newValue)){
							//如果返回true，则会将对象添加到ft中返回
							return true;
						}
						return false;
					}
				});
				//将返回的结果集重新设置到listview中
				listView.setItems(ft);
			}
		});


		//排序
		sortButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				//对listview中的结果集进行排序
				System.out.println("重新排序");
				ObservableList<Student> list=listView.getItems();
				SortedList<Student> sl=list.sorted(new Comparator<Student>() {
					@Override
					public int compare(Student o1, Student o2) {
						return o2.getName().compareTo(o1.getName());
					}
				});
				listView.setItems(sl);
			}
		});


		//返回原来的结果
		anchorPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				listView.setItems(datas);
			}
		});


	}

}
